package com.yuqian.JavaSe.java;

import java.io.UnsupportedEncodingException;

import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

public class ChartSetStudy {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String  s ="1我23";
		System.out.println(s.getBytes("UTF-8").length);
		System.out.println(s.getBytes("gbk").length);
		System.out.println(s.getBytes("gb2312").length);
		
		byte[] re = s.getBytes("gbk");
		System.out.println((int)re[0]);
		ChartSetStudy.subString(s, 1);
		ChartSetStudy.subString(s, 2);
		ChartSetStudy.subStr(s, 4);
		ChartSetStudy.subStr(s, 3);
		ChartSetStudy.subStr(s, 2);
	}
	
	public static void  subString(String source, int byteLength) throws UnsupportedEncodingException {
		if(source==null||"".equals(source)) {
			System.out.println("source is null or empty");
			return ;
		}
		int length = source.length();
		byte[] sByte = source.getBytes("gbk");
		//给出的长度大于字符串长度
		if(byteLength>length) {
			byteLength = length;
		}
		if(sByte[byteLength-1]<0) {
			System.out.println("the result is:"+source.substring(0, byteLength));
		}else {
			System.out.println("the result is:"+source.substring(0, byteLength-1));
		}
	}
	
	 public static void splitString(String src, int len) throws UnsupportedEncodingException {
	        int byteNum = 0;

	        if (null == src) {
	            System.out.println("The source String is null!");
	            return;
	        }

	        byteNum = src.length();
	        byte bt[] = src.getBytes("gbk"); // 将String转换成byte字节数组

	        if (len > byteNum) {
	            len = byteNum;
	        }

	        // 判断是否出现了截半，截半的话字节对于的ASC码是小于0的值
	        if (bt[len-1] < 0) {
	            String subStrx = new String(bt, 0, --len);
	            //System.out.println("subStrx==" + new String(bytes, charset));
	        } else {
	            String subStrx = new String(bt, 0, len);
	            System.out.println("subStrx==" + subStrx);
	        }
	    }
	 
	 /** 
	    * 将给定的字符串按着给定的截取长度截取 
	    * <br> 
	    * 注意一个汉字占2个字节 
	    * @param str 
	    * @param subSLength 
	    * @return 截取后的字符串 
	    * @throws UnsupportedEncodingException  
	    */  
	   public static String subStr(String str, int subSLength)  
	           throws UnsupportedEncodingException  
	   {  
	         
	       if (str == null)  
	           return null;  
	       else  
	       {  
	           int tempSubLength = subSLength;//截取字节数  
	             
	           String subStr = str.substring(0, subSLength);//截取的子串  
	             
	           int subStrByetsL = subStr.getBytes("GBK").length;//截取子串的字节长度  
	             
	           // 说明截取的字符串中包含有汉字  
	           while (subStrByetsL > tempSubLength)  
	           {  
	               subStr = str.substring(0, --subSLength);  
	               subStrByetsL = subStr.getBytes("GBK").length;
	               if(subStrByetsL<tempSubLength) {
	            	   subStr= str.substring(0, 1+subSLength);  
	               }
	           }  
	           System.out.println(subStr);
	           
	           return subStr;  
	       }  
	         
	   }  

}
